package com.css.fxfzdpzs.modules.fwjg.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzdpzs.base.response.RestResponse;
import com.css.fxfzdpzs.modules.fwjg.entity.first.*;
import com.css.fxfzdpzs.modules.fwjg.entity.second.Fwjg5yearsSubmitSituation;
import com.css.fxfzdpzs.modules.fwjg.entity.second.FwjgProjectSituation;
import com.css.fxfzdpzs.modules.fwjg.entity.second.FwjgProjectType;
import com.css.fxfzdpzs.modules.fwjg.entity.second.FwjgSubmitNum;
import com.css.fxfzdpzs.modules.fwjg.entity.third.FwjgViewDetails;
import com.css.fxfzdpzs.modules.fwjg.vo.FwjgInformationSystemVO;
import com.css.fxfzdpzs.modules.fwjg.vo.FwjgProvinceNumVO;
import com.css.fxfzdpzs.modules.fwjg.service.FwjgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author y1fan
 * @description
 */
@RestController
@RequestMapping("/fwjg")
@Slf4j
public class FwjgController {


    @Resource
    private FwjgService fwjgService;


    /**
     1）表名（中文名）：各省（直辖市）提提交房屋数量统计
     表名（英文名）：fwjg_pro_house_num_count
     *
     * @return
     */
    @GetMapping("/getHouseNumCount")
    public RestResponse getHouseNumCount() {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            List<FwjgProHouseNumCount> fwjgProHouseNumCounts = fwjgService.getHouseNumCount();
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(fwjgProHouseNumCounts);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     1）表名（中文名）：房屋加固改造信息服务系统
     表名（英文名）：fwjg_information_system
     *
     * @return
     */
    @GetMapping("/getInformationSystem")
    public RestResponse getInformationSystem() {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            FwjgInformationSystemVO fwjgServiceInformationSystem = fwjgService.getInformationSystem();
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(fwjgServiceInformationSystem);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     1）表名（中文名）：审核采集入库情况
     表名（英文名）：fwjg_process_situation
     *
     * @return
     */
    @GetMapping("/getProcessSituation")
    public RestResponse getProcessSituation() {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            FwjgProcessSituation fwjgProcessSituation = fwjgService.getProcessSituation();
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(fwjgProcessSituation);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     1）表名（中文名）：近五年数据采集情况
     表名（英文名）：fwjg_5years_situation
     *
     * @return
     */
    @GetMapping("/get5yearsSituation")
    public RestResponse get5yearsSituation() {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            List<Fwjg5yearsSituation> fwjg5yearsSituations = fwjgService.get5yearsSituation();
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(fwjg5yearsSituations);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     1）表名（中文名）：房屋设施加固工程典型案例统计
     表名（英文名）：fwjg_province_num
     *
     * @return
     */
    @GetMapping("/getProvinceNum")
    public RestResponse getProvinceNum() {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            FwjgProvinceNumVO fwjgProvinceNumVO = fwjgService.getProvinceNum();
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(fwjgProvinceNumVO);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     1）表名（中文名）：房屋加固典型案例
     表名（英文名）：fwjg_typical_case
     *
     * @return
     */
    @GetMapping("/getTypicalCase")
    public RestResponse getTypicalCase() {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            List<FwjgTypicalCase> list= fwjgService.getTypicalCase();
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(list);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }



    /**
     1）表名（中文名）：  二级    工程类别入库数量排名
     表名（英文名）：fwjg_project_situation
     *
     * @return
     */
    @GetMapping("/getProjectSituation")
    public RestResponse getProjectSituation(String flag) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            List<FwjgProjectSituation> list= fwjgService.getProjectSituation(flag);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(list);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     1）表名（中文名）：  二级    工程类别统计
     表名（英文名）：fwjg_project_type
     *
     * @return
     */
    @GetMapping("/getProjectType")
    public RestResponse getProjectType(String flag) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            List<FwjgProjectType> list= fwjgService.getProjectType(flag);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(list);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     1）表名（中文名）：  二级    近五年典型案例提交情况
     表名（英文名）：fwjg_5years_submit_situation
     *
     * @return
     */
    @GetMapping("/get5yearsSubmitSituation")
    public RestResponse get5yearsSubmitSituation() {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            List<Fwjg5yearsSubmitSituation> list= fwjgService.get5yearsSubmitSituation();
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(list);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     1）表名（中文名）：  二级    提交典型案例数量（Top5）
     表名（英文名）：fwjg_submit_num
     *
     * @return
     */
    @GetMapping("/getSubmitNum")
    public RestResponse getSubmitNum() {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            List<FwjgSubmitNum> list= fwjgService.getSubmitNum();
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(list);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }



    /**
     1）表名（中文名）：  三级    房屋加固典型案例 详细信息
     表名（英文名）：fwjg_view_details
     *
     * @return
     */
    @GetMapping("/getViewDetails/{id}")
    public RestResponse getViewDetails(@PathVariable("id") String id) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            FwjgViewDetails entity= fwjgService.getViewDetails(id);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(entity);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     * 文件下载
     * @param url 附件路径
     */
    @GetMapping("/downLoadFile")
    public void downloadFile(String url,String name) {
        OutputStream os = null;
        InputStream is = null;
        try{

            if (org.apache.commons.lang3.StringUtils.isNotBlank(url)) {
                // 防乱码
//                    String name =
//                        new String(realFileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
                ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                assert requestAttributes != null;
                HttpServletResponse response = requestAttributes.getResponse();
                assert response != null;
                response.reset();
                response.setContentType("application/octet-stream");
                response.setHeader("Content-Disposition", "attachment;filename=\"" + URLEncoder.encode(name+".docx","UTF-8") + "\"");
                // 跨域
                response.setHeader("Access-Control-Allow-Origin", "*");
                response.setHeader("Access-Control-Allow-Method", "POST,GET");
                is = new FileInputStream(new File(url));

                // PrintWrite pw=response.getWriter();  或
                os = response.getOutputStream();
                int flag = 0;
                while ((flag = is.read()) != -1) {
                    os.write(flag);
                }
            }
        } catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
        } finally{
            try{
                if(is != null){
                    is.close();
                }
                if(os != null){
                    os.flush();
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     1）表名（中文名）：  二级    提交典型案例对应地图点
     *
     * @return
     */
    @GetMapping("/getMapViewCase")
    public RestResponse getMapViewCase(@RequestParam(required = false) String type, @RequestParam(required = false) String category) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            List<FwjgMapTypicalCase> mapViewCase = fwjgService.getMapViewCase(type, category);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(mapViewCase);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     1）表名（中文名）：  二级    提交典型案例数量（TOP5）
     表名（英文名）：fwjg_submit_case
     *
     * @return
     */
    @GetMapping("/getSubmitTopNum")
    public RestResponse getSubmitTopNum() {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            List<FwjgMapTypicalCase> mapViewCase = fwjgService.getSubmitTopNum();
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(mapViewCase);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }
}
